package com.parseword2;

public class SimpleParseWord implements ParseWord{

	@Override
	public ParseWordResult parse(ParseParams parseParams) {
		String text = parseParams.getText();
		int length = text.length();
		Word rootNode = parseParams.getRootNode();
		int readEndIndex = 0;
		int wordSartIndex = 0;	// 下一字
		for (int i = 0; i < length; i++) {
			char c = text.charAt(i);
			
			Word node = rootNode.get(c);
			if(node == null){
				if(readEndIndex>0){
					break;
				}
				// 当前词空为查询到从下一子开始
				node = parseParams.getRootNode();
				i = wordSartIndex;
				wordSartIndex++;
			}else{
				if(node.isEnd()){
					readEndIndex = i+1;
				}
			}
			rootNode = node;
		}
		if(readEndIndex == 0 ){
			return new ParseWordResult(text, null, null);
		}
		String frontText = text.substring(0,wordSartIndex);			// 前面的文本
		String wordText = text.substring(wordSartIndex,readEndIndex);	// 截取词汇
		String rearText = text.substring(readEndIndex);					// 后面的文本
		
		return new ParseWordResult(frontText, wordText, rearText);
	}

}
